<template>
  <el-scrollbar height="100vh">
    <div class="container">
      <el-affix :offset="1">
        <h1 class="title">限制图层的最大/最小分辨率 minResolution / maxResolution</h1>
      </el-affix>
      <div id="map" class="map-x" ref="mapBox"></div>
      <dl>
        <dt>限制图层的最大/最小分辨率 minResolution / maxResolution</dt>
        <dd>在限制的范围之内时显示图层，在限制的范围之外时不显示图层</dd>
        <dd>只有当指定图层在 minResolution 和 maxResolution 范围内，才显示。</dd>
      </dl>
      <div class="codestyle"><Code /></div>
    </div>
  </el-scrollbar>
</template>
<script>
import { toRefs, reactive, ref, onMounted } from 'vue-demi'
import Code from './code.vue'
import 'ol/ol.css'
import { Map, View } from 'ol'
import OSM from 'ol/source/OSM'
import Tile from 'ol/layer/Tile'
import XYZ from 'ol/source/XYZ'
export default {
  setup () {
    const state = reactive({
      map: ref(null),
      mapBox: ref(null)
    })

    const init = () => {
      state.map = new Map({
        target: state.mapBox,
        layers: [
          new Tile({
            source: new OSM(), // 图层数据源
            // 当图层不在 2000 至 20000 时，将不显示该图层
            minResolution: 2000,
            maxResolution: 20000
          }),
          new Tile({
            name: 'tdroad',
            source: new XYZ({
              url: 'http://t4.tianditu.com/DataServer?T=vec_w&x={x}&y={y}&l={z}&tk=ab47b4591ab6e07e0530606eca973edd'
            })
          })
        ],
        view: new View({
          // 地图视图
          projection: 'EPSG:4326', // 坐标系，有EPSG:4326和EPSG:3857
          center: [116.41400, 39.91500],
          zoom: 7 // 地图缩放级别（打开页面时默认级别）
        })
      })
    }

    onMounted(() => {
      init()
    })

    return {
      init,
      ...toRefs(state)
    }
  },
  components: {
    Code
  }
}
</script>
<style lang="scss" scoped>
@import '../../css/main.scss';
.el-row {
  margin-top: 20px;
}
.tit {
  display: block;
  margin-bottom: 10px;
}
</style>
